#include "DataManager.hh"
#include <fstream>
#include "../Utils/include/json.hpp"
#include "Room.hh"
#include <iostream>
#include <cstdlib> // 用于rand()
#include <ctime>   // 用于time()
using namespace std;

using json = nlohmann::json;

// 构造函数,初始化随机数种子
DataManager::DataManager() {
    srand(time(0));
}

// 析构函数,释放所有房间对象
DataManager::~DataManager() {
    for (auto& pair : rooms_) {
        delete pair.second;
    }
}

// 从文件加载所有物品
void DataManager::loadAllItems(const std::string& filename) {
    std::ifstream file(filename);
    json j;
    file >> j;
    for (auto& elem : j) {
        Item temp = elem.get<Item>();
        items_[temp.getID()] = temp;
    }
}

// 从文件加载所有敌人
void DataManager::loadAllEnemies(const std::string& filename) {
    std::ifstream file(filename);
    json j;
    file >> j;
    for (auto& elem : j) {
        Enemy temp = elem.get<Enemy>();
        enemies_[temp.getID()] = temp;
    }
}

// 从文件加载所有房间
void DataManager::loadAllRooms(const std::string& filename) {
    std::ifstream file(filename);
    json j;
    file >> j;
    for (auto& elem : j) {
        int id = elem["id"];
        int floor = elem["floor"];
        std::string type = elem["type"];
        std::string desc = elem["description"];

        // 根据房间类型创建不同的房间对象
        if (type == "normal") {
            rooms_[id] = new NormalRoom(id, desc, floor);
        }
        else if(type == "exit") {
            int nextFloor = elem.value("next_floor", 0);
            rooms_[id] = new ExitRoom(id, desc, nextFloor, floor);
        }
        else if (type == "item") {
            rooms_[id] = new ItemRoom(id, desc, items_, floor);
        }
        else if (type == "monster") {
            int monsterID = elem["monster"];
            int reward = elem["reward"];
            rooms_[id] = new MonsterRoom(id, desc, &enemies_[monsterID], reward, floor);
        }
        else if (type == "boss") {
            int monsterID = elem["monster"];
            int reward = elem["reward"];
            rooms_[id] = new MonsterRoom(id, desc, &enemies_[monsterID], reward, floor);
        }
        else if (type == "healing") {
            rooms_[id] = new HealingRoom(id, desc, floor);
        }
    }
}

// 获取所有物品的引用
const std::map<int, Item>& DataManager::getAllItems() const { return items_; }

// 获取所有敌人的引用
const std::map<int, Enemy>& DataManager::getAllEnemies() const { return enemies_; }

// 根据ID获取房间指针
IRoom* DataManager::getRoom(int id) const {
    auto it = rooms_.find(id);
    if (it != rooms_.end()) {
        // 出口房间不检查楼层
        if (id < 0) {
            return it->second;
        }
        // 普通房间检查楼层
        if (it->second->getFloor() == currentFloor_) {
            return it->second;
        }
    }
    return nullptr;
}

// 从文件加载所有玩家角色
std::vector<Player> DataManager::loadPlayers(const std::string& filename) {
    std::ifstream file(filename);
    json j;
    file >> j;

    std::vector<Player> players;
    for (auto& elem : j) {
        Player temp = elem.get<Player>();
        players.push_back(temp);
    }
    return players;
}

// 让用户选择玩家角色
Player DataManager::choosePlayer(const std::vector<Player>& players) {
    std::cout << "Please select your character:" << std::endl;
    for (size_t i = 0; i < players.size(); ++i) {
        std::cout << i + 1 << ". " << players[i].getName() << std::endl;
    }

    int choice;
    std::cin >> choice;

    while (choice < 1 || choice > (int)players.size()) {
        std::cout << "Invalid choice, please re-enter:" << std::endl;
        std::cin >> choice;
    }

    std::cout << "You selected: " << players[choice - 1].getName() << std::endl;
    return players[choice - 1];
}

// 加载所有楼层地图
void DataManager::loadAllFloors(const std::vector<std::vector<std::vector<int>>>& floors) {
    floors_ = floors;
}

// 获取指定楼层的地图
const std::vector<std::vector<int>>& DataManager::getFloorMap(int floorIndex) const {
    if (floorIndex < 1 || floorIndex > floors_.size()) {
        throw std::invalid_argument("Invalid floor index");
    }
    return floors_[floorIndex - 1];
}
